// pages/profile/interests-skills/index.ts
import tran from '../../../utils/language';
import { commonTagList } from '../../../utils/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _t: tran._t('interestsSkills'),
    _tBase: tran._t('base'),
    checked: [''],
    professionAndInterestList: [
      {
        selectFlag: false,
        tagName: "",
        tagType: "",
        tagTypeId: ""
      }],
    type: 'interest',
    loginInfo: {
      interestIds: [],
      skills: [],
    },
    isEdit: getApp().globalData.profileFrom === 'edit'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: any) {
    const loginInfo = wx.getStorageSync('userInfo')
    if (options && Object.keys(options).length > 0) {
      this.setData({
        type: options.type,
        loginInfo
      })
      const interestList = wx.getStorageSync('interestList')
      const professionList = wx.getStorageSync('professionList')
      if (options.type == 'interest') {
        if (!interestList || interestList.length <= 0) {
          this.getInterest()
        }
        this.setData({
          professionAndInterestList: interestList
        }, () => {
          this.handleData()
        })
      } else {
        if (!professionList || professionList.length <= 0) {
          this.getProfession()
        }
        this.setData({
          professionAndInterestList: professionList
        }, () => {
          this.handleData()
        })
      }
    }
  },

  /**
   * 处理编辑的时候回显的数据
   */
  handleData() {
    const { loginInfo, professionAndInterestList, type } = this.data
    const userInterest: any = loginInfo.interestIds
    const userProfession: any = loginInfo.skills
    if (type == 'interest') { // 兴趣
      professionAndInterestList.map((item: any) => {
        if (userInterest && userInterest.length > 0) {
          const list = userInterest.filter((item1: any) => item1 == item.tagTypeId)
          if (list.length > 0) {
            item.selectFlag = true
          }
        }
      })

    } else { // 职业/技能
      professionAndInterestList.map((item: any) => {
        if (userProfession && userProfession.length > 0) {
          const list = userProfession.filter((item1: any) => item1.tagTypeId == item.tagTypeId)
          if (list.length > 0) {
            item.selectFlag = true
          }
        }

      })

    }
    this.setData({
      professionAndInterestList
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const { type } = this.data
    if (type == 'interest') {
      const interestList = wx.getStorageSync('interestList')
      if (interestList && interestList.length > 0) {
        this.setData({
          professionAndInterestList: interestList
        })
      }
    } else {
      const professionList = wx.getStorageSync('professionList')
      if (professionList && professionList.length > 0) {
        this.setData({
          professionAndInterestList: professionList
        })
      }
    }
  },
  confirmTap() {
    const { type, professionAndInterestList } = this.data
    // const currentList = professionAndInterestList.filter(item => item.selectFlag == true)
    // 将选中的放进缓存里面
    // wx.setStorageSync('interestList', currentList);
    if (type == 'interest') {
      wx.setStorageSync('interestList', professionAndInterestList);
    } else {
      wx.setStorageSync('professionList', professionAndInterestList);
    }
    setTimeout(() => {
      wx.navigateBack()
    }, 100)

  },
  /**
   * 获取兴趣列表
   */
  getInterest() {
    let params = {
      tagType: 'INTEREST'
    }
    commonTagList(params).then(res => {
      res.length > 0 && res.map((item: any) => {
        item.text = item.tagName
        item.selectFlag = false
      })
      this.setData({
        professionAndInterestList: res
      }, () => {
        this.handleData()
      })
    })
  },
  /**
   * 获取职业技能列表
   */
  getProfession() {
    let params = {
      tagType: 'SKILL'
    }
    commonTagList(params).then(res => {
      let list: any = []
      res.length > 0 && res.map((item: any) => {
        item.text = item.tagName
        item.selectFlag = false
        item.value = item.tagTypeId
        list.push(item)
      })
      this.setData({
        professionAndInterestList: list
      }, () => {
        this.handleData()
      })
    })
  },
  changeSelect(e: any) {
    const { info } = e.currentTarget.dataset
    console.log(info,'点击的数据');
    const { professionAndInterestList } = this.data
    // 判断用户点击的是哪一个按钮，从而将那一个按钮状态反选
    professionAndInterestList.map(item => {
      if (item.tagName === info.tagName) {
        item.selectFlag = !item.selectFlag
      }
    })
    this.setData({
      professionAndInterestList
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})


